package com.ylkj.cloud;

import java.util.*;
import java.util.concurrent.*;

public class MultiThreadWithMapListMapParam {
    private static final int PAGE_SIZE = 1000;
    private static final int THREAD_POOL_SIZE = 4;

    public static void main(String[] args) {
        final long l = System.currentTimeMillis();
        // 准备 Map<String, List<Map<String, Object>>> 类型的参数
        Map<String, List<Map<String, Object>>> paramMap = prepareParam();

        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
        List<Future<Map<String, List<Map<String, Object>>>>> futures = new ArrayList<>();

        // 模拟多个数据处理任务，传入参数
        List<Map<String, List<Map<String, Object>>>> listPar = new ArrayList<>();
        for (int i = 0; i < THREAD_POOL_SIZE; i++) {
            List<Map<String, Object>> group1 = paramMap.get("group1");
            group1.get(0).put("i",i);
            Map<String, List<Map<String, Object>>> params = new HashMap<>();
            Map<String, Object> group11 = paramMap.get("group1").get(0);

            HashMap<String, Object> filter = new HashMap<>(group11);
            filter.put("i",i);
             ArrayList<Map<String, Object>> maps = new ArrayList<>();
            maps.add(filter);
            params.putAll(paramMap);
            DataProcessor dataProcessor = new DataProcessor(params, i);
            params.put("group1",maps);
            listPar.add(params);

            futures.add(executorService.submit(dataProcessor));
        }
        System.out.println("listPar = " + listPar);

        // 汇总所有线程处理后的数据
        Map<String, List<Map<String, Object>>> allData = new HashMap<>();
        for (Future<Map<String, List<Map<String, Object>>>> future : futures) {
            try {
                final Map<String, List<Map<String, Object>>> result = future.get();
                for (Map.Entry<String, List<Map<String, Object>>> entry : result.entrySet()) {
                    String key = entry.getKey();
                    List<Map<String, Object>> list = entry.getValue();
                    allData.computeIfAbsent(key, k -> new ArrayList<>()).addAll(list);
                }
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }

        // 关闭线程池
        executorService.shutdown();

        // 将 Map<String, List<Map<String, Object>>> 中的数据汇总到一个大的 List<Map<String, Object>> 中
        List<Map<String, Object>> flatData = new ArrayList<>();
        for (List<Map<String, Object>> list : allData.values()) {
            flatData.addAll(list);
        }

        // 分页处理数据
        List<List<Map<String, Object>>> paginatedData = paginate(flatData,PAGE_SIZE);
        System.out.println("总页数: " + paginatedData.size());

        final long l1 = System.currentTimeMillis();
        System.out.println("耗时："+ (l1-l)/1000.0+"秒");
        for (int i = 0; i < paginatedData.size(); i++) {

            final List<Map<String, Object>> maps = paginatedData.get(i);
            for (int i1 = 0; i1 < maps.size(); i1++) {
                final Map<String, Object> stringObjectMap = maps.get(i1);
                final Object taskNumber = stringObjectMap.get("taskNumber");
                System.out.println("第 " + (i1 + 1) + " 页的记录+"+"taskNumber = " + taskNumber);

            }

        }
    }

    private static Map<String, List<Map<String, Object>>> prepareParam() {
        Map<String, List<Map<String, Object>>> paramMap = new HashMap<>();
        List<Map<String, Object>> list1 = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();

        map1.put("key1", "value1");
        list1.add(map1);
        paramMap.put("group1", list1);
        return paramMap;
    }

    public static <T> List<List<T>> paginate(List<T> dataList,Integer pageSize) {
        List<List<T>> paginatedList = new ArrayList<>();
        int totalSize = dataList.size();
        for (int i = 0; i < totalSize; i += pageSize) {
            int endIndex = Math.min(i + pageSize, totalSize);
            List<T> pageData = dataList.subList(i, endIndex);
            paginatedList.add(pageData);
        }
        return paginatedList;
    }

    static class DataProcessor implements Callable<Map<String, List<Map<String, Object>>>> {
        private  Map<String, List<Map<String, Object>>> inputParam;
        private  int taskNumber;

        public DataProcessor(Map<String, List<Map<String, Object>>> inputParam, int taskNumber) {
            this.inputParam = inputParam;
            this.taskNumber = taskNumber;
        }

        @Override
        public Map<String, List<Map<String, Object>>> call() throws InterruptedException {
            List<Map<String, Object>> group1 = inputParam.get("group1");
            Integer i = (Integer) group1.get(0).get("i");
            System.out.println("当前线程耗时"+i+"秒");
            Thread.sleep(i*1000);
//            System.out.println("i = " + i);

//            // 这里可以对传入的 inputParam 进行处理
            Map<String, List<Map<String, Object>>> resultMap = new HashMap<>();
            for (Map.Entry<String, List<Map<String, Object>>> entry : inputParam.entrySet()) {
                String key = entry.getKey();
                List<Map<String, Object>> list = entry.getValue();
                List<Map<String, Object>> newList = new ArrayList<>();
                for (Map<String, Object> map : list) {
                    Map<String, Object> newMap = new HashMap<>(map);
                    newMap.put("taskNumber", taskNumber);
                    newList.add(newMap);
                }
                resultMap.put(key, newList);
            }
            return resultMap;
        }
    }
}